********************************************************************************
*Project:  	Swiss Trade During the Covid-19 Pandemic: An Early Appraisal	
*Authors:	KB, SL, VP, PW										
*This File: Clean Classifications on Country Codes of UNCTAD
*Version: 	vFinal						
*Date: 		2020-09-01						
********************************************************************************

********************************************************************************
*1. Modify various entries that are duplicates in main iso2 UNCTAD file
********************************************************************************

use "${root}\temp\UNCTAD_country_groups_raw.dta", clear

*MODIFY TRADE GROUPS
********************

*Svalbard and Jan Mayen: NO PERMANENT POPULATION
foreach var in EFTA EU_EFTA_2020 {
	replace `var'=1 if land_UNCTAD==579001

}
*Liechtenstein
foreach var in EFTA EU_EFTA_2020 {
	replace `var'=1 if land_UNCTAD==757001
}

*UK
foreach var in EU2020 EFTA EU_EFTA_2020 EU15 {
	replace `var'=1 if land_UNCTAD==926

}

*MODIFY GEOGAPHIC / REGIONAL GROUPS
************************************

*Åland Islands
foreach var in land_region6 land_region7 {
	replace `var'="Europe" if land_UNCTAD==246001
}

*Guadeloupe, Martinique,  French Guiana
replace land_region6="America" if land_UNCTAD==251001
replace land_region7="Latin America" if land_UNCTAD==251001

replace land_region6="America" if land_UNCTAD==251004
replace land_region7="Latin America" if land_UNCTAD==251004

replace land_region6="America" if land_UNCTAD==251006
replace land_region7="Latin America" if land_UNCTAD==251006

*Myotte, Réunion
replace land_region6="Africa" if land_UNCTAD==251002
replace land_region7="Africa" if land_UNCTAD==251002

replace land_region6="Africa" if land_UNCTAD==251003
replace land_region7="Africa" if land_UNCTAD==251003

*Monaco
foreach var in land_region6 land_region7 {
	replace `var'="Europe" if land_UNCTAD==251005
}


*Svalbard and Jan Mayen
foreach var in land_region6 land_region7 {
	replace `var'="Europe" if land_UNCTAD==579001
}

*Liechtenstein
foreach var in land_region6 land_region7 {
	replace `var'="Europe" if land_UNCTAD==757001
}

*Puerto Rico & Virgin Islands
replace land_region6="America" if land_UNCTAD==842001
replace land_region7="Latin America" if land_UNCTAD==842001

replace land_region6="America" if land_UNCTAD==842002
replace land_region7="Latin America" if land_UNCTAD==842002

*Guernsey, Jersey, Isle of Man
foreach var in land_region6 land_region7 {
	replace `var'="Europe" if land_UNCTAD>=926001 & land_UNCTAD<=926002
}

*Antarctica
replace land_region6="Antarctica" if merge_geopraphic_groups==1 & land_region6==""
replace land_region7="Antarctica" if merge_geopraphic_groups==1 & land_region7==""

********************************************************************************
*2. Add entries for trade partners listed in FCA data but not in UNCTAD data 
********************************************************************************

*ADD REGIONAL UNITS THAT ARE IN FCA TRADE DATA
***********************************************
count
local N=r(N)+1

insobs 1
replace land_UNCTAD = 999001 in `N'
replace land_iso2 = "XC" in `N'
replace land_str = "Ceuta" in `N'
replace land_region6="Africa" in `N'
replace land_region7="Africa" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999002 in `N'
replace land_iso2 = "XL" in `N'
replace land_str = "Melilla" in `N'
replace land_region6="Africa" in `N'
replace land_region7="Africa" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999003 in `N'
replace land_iso2 = "XB" in `N'
replace land_str = "Ceuta and Melilla" in `N'
replace land_region6="Africa" in `N'
replace land_region7="Africa" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999004 in `N'
replace land_iso2 = "XA" in `N'
replace land_str = "Kanarische Inseln" in `N'
replace land_region6="Africa" in `N'
replace land_region7="Africa" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999005 in `N'
replace land_iso2 = "XK" in `N'
replace land_str = "Kosovo" in `N'
replace land_region6="Europe" in `N'
replace land_region7="Europe" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999006 in `N'
replace land_iso2 = "CS" in `N'
replace land_str = "Serbien und Montenegro" in `N'
replace land_region6="Europe" in `N'
replace land_region7="Europe" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999007 in `N'
replace land_iso2 = "YU" in `N'
replace land_str = "Jugoslawien" in `N'
replace land_region6="Europe" in `N'
replace land_region7="Europe" in `N'

local N=`N'+1
insobs 1
replace land_UNCTAD = 999008 in `N'
replace land_iso2 = "AN" in `N'
replace land_str = "ex Niedr. Antillen" in `N'
replace land_region6="America" in `N'
replace land_region7="Latin America" in `N'

foreach var in EU2020 Eurozone NAFTA EU15 EU25 EU27 EU28 EFTA EU_EFTA_2020 {
	replace `var'=0 if `var'==.
}

saveold "${root}\temp\UNCTAD_country_groups.dta", replace version(13)
